function Installer(options)
{
	var _self = this;
	
	this.options = {
		sOverallProgressbarId: 'overallProgress',
		oOverallProgressbar: null,
		iOverallProgressStep: 1,
		iModuleCounter: 0,
		sCurrentModule: '',
		bFinish: false
	}
	
	this.Init = function(options){
		_self.options = $.extend(_self.options, options);
	}
	
	this.ThrowError = function(data){
		error = '';
		if (typeof(data)=='object'&&(data instanceof Array))
		{
			for (i=0;i<data.length;i++)
			{
				error += data[i]+"<br>";
			}
		}
		else
		{
			error = data;
		}
		error = "<div class='error'>"+error+"</div>";
		throw error;
	}
	
	this.ModuleStart = function(){
		$.ajax({
			url: 'index.php?r=install/default/ajaxstart',
			type: 'POST',
			dataType: 'json',
			async: false,
			cache: false,
			success: function(data){
//				setTimeout(function(){
					if (data.error)
					{
						_self.ThrowError(data.error);
					}
					else
					{
						$('#currentModule').html(data.html);
						_self.options = $.extend(_self.options, {sCurrentModule:data.module});
						$('#'+_self.options.sCurrentModule+'_install').progressbar({'value':0});
					}
//				}, 500);
			}
		});
	}
	
	this.SetAutoload = function(){
		$.ajax({
			url: 'index.php?r=install/default/ajaxsetautoload',
			data: {'module':_self.options.sCurrentModule},
			type: 'GET',
			dataType: 'json',
			async: false,
			cache: false,
			success: function(data){
//				setTimeout(function(){
					if (data.error)
					{
						_self.ThrowError(data.error);
					}
					else
					{
						$('#installProcess').html(data.html);
						$('#'+_self.options.sCurrentModule+'_install').progressbar('option', 'value', 25);
					}
//				}, 500);
			}
		});
	}
	
	this.InstallDatabase = function(){
		$.ajax({
			url: 'index.php?r=install/default/ajaxinstalldatabase',
			data: {'module':_self.options.sCurrentModule},
			type: 'GET',
			dataType: 'json',
			async: false,
			cache: false,
			success: function(data){
//				setTimeout(function(){
					if (data.error)
					{
						_self.ThrowError(data.error);
					}
					else
					{
						$('#installProcess').html(data.html);
						$('#'+_self.options.sCurrentModule+'_install').progressbar('option', 'value', 50);
					}
//				}, 500);
			}
		});
	}
	
	this.FinishModule = function(){
		$.ajax({
			url: 'index.php?r=install/default/ajaxfinishmodule',
			data: {'module':_self.options.sCurrentModule},
			type: 'GET',
			dataType: 'json',
			async: false,
			cache: false,
			success: function(data){
//				setTimeout(function(){
					if (data.error)
					{
						_self.ThrowError(data.error);
					}
					else
					{
						$('#installProcess').html(data.html);
						$('#'+_self.options.sCurrentModule+'_install').progressbar('option', 'value', 100);
						_self.options = $.extend(_self.options, {bFinish:data.finish, iModuleCounter:_self.options.iModuleCounter+1});
					}
//				}, 500);
			}
		});
	}
	
	this.InstallModule = function(){
		_self.ModuleStart();
		_self.SetAutoload();
		_self.InstallDatabase();
		_self.FinishModule();
		$('#'+_self.options.sOverallProgressbarId).progressbar('option', 'value', _self.options.iModuleCounter*_self.options.iOverallProgressStep);
					
		return _self.options.bFinish;
	}
	
	this.Start = function(){
		// initialize overallProgressbar
		$('#'+_self.options.sOverallProgressbarId).progressbar({'value':0});
		
		var _installDone = false;
		while(!_installDone){
			try
			{
				_installDone = _self.InstallModule();
			}
			catch(error)
			{
				_installDone = false;
				$('#installProcess').append(error);
				break;
			}
		}
		if (_installDone)
			jQuery('#finishbutton').button('option', 'disabled', false)
		
		console.log('Finished instaling modules');
	}
	
	_self.Init(options);
}

